import { _decorator, CCString, Component, Label, Node, RichText, tween, UIOpacity, Vec3, } from 'cc'
import { Logger } from 'db://assets/scripts/utils/log'
import { tweenConfig } from 'db://assets/scripts/enum'
const { ccclass, property } = _decorator

@ccclass('NaratorContent')
export class NaratorContent extends Component {
    @property({
        tooltip: '对话内容',
        displayName: '对话内容',
        type: RichText,
    })
    narratorContent: RichText

    @property({
        tooltip: '预设内容',
        displayName: '预设内容',
    })
    context: string = ''

    #log = new Logger(`旁白弹窗[${this.name}]`)

    protected onLoad() {
        this.#log.debug("*加载完成*")
        this.node.active = false
    }

    protected onDestroy(): void {
        this.#log.debug('*组件销毁*')
    }

    protected onEnable(): void {
        this.#log.debug('*组件启用*')
        this.showDialog(() => {
            this.refreshContext()
        })
    }

    protected onDisable(): void {
        this.#log.debug('*组件禁用*')
        this.hideDialog()
    }

    refreshContext = (cb = () => {}) => {
        this.narratorContent.string = ''
        this.schedule(() => {
            this.narratorContent.string += this.context[this.narratorContent.string.length]
            if(this.narratorContent.string == this.context) cb()
        }, 0.02, this.context.length - 1)
    }


    showDialog(call = () => {}){
        this.#log.debug('::showDialog() 展示弹窗', this.node)
        const t2 = tween(this.node.getComponent(UIOpacity)).to(tweenConfig.params.duration.DURATION_2, { opacity: 255 })
        tween(this.node).parallel(t2).call(call).start()
    }

    hideDialog(call = () => {}) {
        this.#log.debug('::hideDialog() 隐藏弹窗')
        const t2 = tween(this.node.getComponent(UIOpacity)).to(tweenConfig.params.duration.DURATION_2, { opacity: 0 })
        tween(this.node).parallel(t2).call(call).start()
    }
}
